package main

import (
	"fmt"
	proxy2 "gin-gateway/example/gateway_demo/otherProxy/proxy"
	"google.golang.org/grpc"
	"log"
	"net"
	"gin-gateway/example/gateway_demo/otherProxy/load_balance"
	"gin-gateway/example/gateway_demo/otherProxy/grpc_proxy"
	"google.golang.org/grpc/encoding"
)

const port = ":50051"

func main() {
	Listen, err := net.Listen("tcp", port)
	if err != nil {
		log.Fatalf("failed to listen: %v", err)
	}

	lb := load_balance.LoadBanlanceFactory(load_balance.LbWeightRoundRobin)
	lb.Add("127.0.0.1:50055", "40")
	encoding.RegisterCodec(grpc_proxy.Codec()) // 设置grpcServer 全局使用编解码

	grpcHandler := proxy2.NewGrpcLoadBalanceHandler(lb)
	grpcServer := grpc.NewServer(grpc.UnknownServiceHandler(grpcHandler)) //自定义全局回调

	fmt.Printf("server listening at %v\n", Listen.Addr())
	if err := grpcServer.Serve(Listen); err != nil {
		log.Fatalf("failed to serve: %v", err)
	}
}
